package com.boang.controller.house;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boang.entity.Room;
import com.boang.entity.VO.RoomVO;
import com.boang.service.RoomService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Tag(name = "房屋HouseController")
@RestController
@RequestMapping("/house")
public class HouseController {
    @Autowired
    private RoomService roomService;

    @PutMapping("/addRoom")
    public boolean addRoom(@RequestBody Room room) {
        return roomService.save(room);
    }

    @PutMapping("/updateRoom")
    public boolean updateRoom(@RequestBody Room room) {
        return roomService.updateById(room);
    }

    @DeleteMapping("/deleteRoom")
    public boolean deleteRoom(@RequestParam("id") Integer id) {
        return roomService.removeById(id);
    }

    @PostMapping("/page")
    public IPage<Room> page(@RequestBody RoomVO room) {
        // 1. 分页参数校验
        if (room.getPageNum() < 1) room.setPageNum(1);
        if (room.getPageSize() < 1 || room.getPageSize() > 100) room.setPageSize(10);
        IPage<Room> page = new Page<>(room.getPageNum(), room.getPageSize());
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(room.getState() != null, Room::getState, room.getState())
                .like(room.getType() != null, Room::getType, room.getType())
                .like(room.getUnitId() != null, Room::getUnitId, room.getUnitId());
        return roomService.page(page, queryWrapper);
    }

}
